package com.card.match;

import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;

import com.card.common.CardContants;
import com.card.common.MessageComm;
import com.card.game.room.AbstractRoom;
import com.card.log.LogType;
import com.card.log.LogUtils;
import com.card.message.JsonMessageCenter;
import com.card.user.User;
import com.card.user.UserCurrency.CurrencyKind;
import com.xiaoao.net.json.IMsgProcesser;
import com.xiaoao.net.json.Router;
import com.xiaoao.net.json.message.JsonMessage;

/**
 * 用户聊天相关管理类
 * 
 * @author Administrator
 *
 */
public class MatchLogic implements IMsgProcesser {
	private static final MatchLogic instance = new MatchLogic();

	public static MatchLogic getInstance() {
		return instance;
	}
	
	@Override
	public void registMsgId() {
		Router.getInstance().registMsg(MessageComm.C2S_Join_Match, this);
		Router.getInstance().registMsg(MessageComm.C2S_Match_Start, this);
		Router.getInstance().registMsg(MessageComm.C2S_Match_List, this);
		Router.getInstance().registMsg(MessageComm.C2S_Match_Leave, this);
		Router.getInstance().registMsg(MessageComm.C2S_Match_GiveUp, this);
	}

	/**
	 * 消息逻辑处理
	 */
	@Override
	public void doProcess(int msgId, JsonMessage msg, ChannelHandlerContext ctx) {
		switch (msgId) {
			case MessageComm.C2S_Join_Match:
				enterMatch(msg, ctx);
				break;
			case MessageComm.C2S_Match_Start:
//				enterMatchRoom(msg, ctx);
				break;
			case MessageComm.C2S_Match_List:
				matchList(msg, ctx);
				break;
			case MessageComm.C2S_Match_Leave:
				matchLeave(msg, ctx);
				break;
			case MessageComm.C2S_Match_GiveUp:
				matchGiveUp(msg, ctx);
				break;
		}
	}

	private void enterMatch(JsonMessage msg, ChannelHandlerContext ctx) {
		User user = (User) ctx.getAttachment();
		AbstractRoom room = user.getRoom();
		if(room != null) return;
		int matchId = msg.getAsInt("matchId");
		if(user.getMatch() != null) {
			JsonMessageCenter.getInstance().sendEorrorMessage(user, "您已经报名参加比赛了,无法重复参赛");
			return;
		}
		Match match = MatchManager.getInstance().getMatch(matchId);
		if(match == null) {
			JsonMessageCenter.getInstance().sendEorrorMessage(user, "比赛已经关闭,无法参赛");
			return;
		}
		if(match.isContains(user.getPrimaryKey())) {
			JsonMessageCenter.getInstance().sendEorrorMessage(user, "您已经报名参加比赛了,无法重复参赛");
			return;
		}
		if(match.isStart()) {
			JsonMessageCenter.getInstance().sendEorrorMessage(user, "比赛已经开始");
			return;
		}
		int spend = match.getSpend();
		if(!user.userCurrency().spend(CurrencyKind.Diamonds, spend)) {
			JsonMessageCenter.getInstance().sendEorrorMessage(user, "房卡不足,无法报名");
			return;
		}
			
		//这是看报名的是哪个比赛
		//麻将
		if(match.getGameId() == CardContants.GAMETYPE_SH) {
			LogUtils.addSpendLog(user, LogType.CMMJ, spend);
			//斗地主
		}else if(match.getGameId() == CardContants.GAMETYPE_Ddz) {
			LogUtils.addSpendLog(user, LogType.CMDDZ, spend);
			//炸金花
		}else if(match.getGameId() == CardContants.GAMETYPE_TGG) {
			LogUtils.addSpendLog(user, LogType.BMZJH, spend);
			//牛牛
		}else if(match.getGameId() == CardContants.GAMETYPE_Nn) {
			LogUtils.addSpendLog(user, LogType.BMNN, spend);
		}else if(match.getGameId() == CardContants.GAMETYPE_Pdk) {
		//跑得快
			LogUtils.addSpendLog(user, LogType.CMPDK, spend);
		}
		user.sendUserUpdateMessage();
		//信息发送
		JsonMessage message = new JsonMessage(MessageComm.S2C_Join_Match);
		message.addProperty("matchId", matchId);
		JsonMessageCenter.getInstance().sendMessage(message, ctx);
		match.addUser(user);
		
		MatchManager.getInstance().broadMatchUpdate();
	}
	//进入比赛房间
	private void enterMatchRoom(JsonMessage msg, ChannelHandlerContext ctx) {
		User user = (User) ctx.getAttachment();
		IMatchRoom room = (IMatchRoom)user.getRoom();
		if(room == null) return;
		room.enterMatchRoom(user.getPrimaryKey());
	}
	//列表
	private void matchList(JsonMessage msg, ChannelHandlerContext ctx) {
		JsonMessage message = JsonMessage.createMessage(MessageComm.S2C_Match_List);
		MatchManager.getInstance().fillMsg(message);
		JsonMessageCenter.getInstance().sendMessage(message, ctx);
	}
	//离开比赛
	private void matchLeave(JsonMessage msg, ChannelHandlerContext ctx) {
		User user = (User) ctx.getAttachment();
		Match match = user.getMatch();
		if(match != null) {
			if(match.isStart()) {
				JsonMessageCenter.getInstance().sendEorrorMessage(user, "比赛已经开始不能退出");
				return;
			}
			match.matchLeave(user.getPrimaryKey());
			
			int spend = match.getSpend();
			//增加钻石&金币
			user.userCurrency().add(CurrencyKind.Diamonds, spend);
			//离开比赛的信息
			if(match.getGameId() == CardContants.GAMETYPE_SH) {
				LogUtils.addSpendLog(user, LogType.CMMJ2, spend);
			}else if(match.getGameId() == CardContants.GAMETYPE_Ddz) {
				LogUtils.addSpendLog(user, LogType.CMDDZ2, spend);
			}
			else if(match.getGameId() == CardContants.GAMETYPE_TGG) {
				LogUtils.addSpendLog(user, LogType.QXZJH, spend);
			}else if(match.getGameId() == CardContants.GAMETYPE_Nn) {
				LogUtils.addSpendLog(user, LogType.QXNN, spend);
			}else if(match.getGameId() == CardContants.GAMETYPE_Pdk){
				LogUtils.addSpendLog(user, LogType.CMPDK2, spend);
			}
			user.sendUserUpdateMessage();
		}
	}
	//放弃比赛
	private void matchGiveUp(JsonMessage msg, ChannelHandlerContext ctx) {
		User user = (User) ctx.getAttachment();
		Match match = user.getMatch();
		boolean isSuc = false;
		if(match != null) {
			isSuc = match.leaveMatch(user.getPrimaryKey());
		}
		JsonMessage message = JsonMessage.createMessage(MessageComm.S2C_Match_GiveUp);
		message.addProperty("isSuc", isSuc);
		JsonMessageCenter.getInstance().sendMessage(message, ctx);
	}
}
